package com.ithema.edu.info.manager.service;

import com.ithema.edu.info.manager.dao.StudentDao2;
import com.ithema.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class StudentService {
    private final StudentDao2 studentDao = new StudentDao2();

    public  void addStudents(Student student) {
        studentDao.addStudent(student);
    }

    public  ArrayList<Student> queryStudents() {
        return studentDao.queryStudents();
    }

    public  int isExists(String id) {
        ArrayList<Student> arrayList = studentDao.queryStudents();
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getId().equals(id)) {
                return i;
            }

        }
        return -1;
    }

    public  void updateStudent(int index, Student newstudent) {
        studentDao.updateStudent(index, newstudent);
    }

    public  boolean deleteStudent(String id) {
        int index = isExists(id);
        if (index >= 0) {
            studentDao.deleteStudent(index);
            return true;
        }
        return false;
    }
}
